package com.ieslab.linear;

/**
 * 栈与递归
 */
public class StackAndRecurision {
	public static void main(String[] args) {
		System.out.println("fib(3)=" + fib(3));
	}

	/**
	 * 计算斐波那契数列
	 * 
	 * @param n
	 *            Fib(n)的参数n
	 * @return Fib(n)的值
	 */
	private static int fib(int n) {
		if (n == 0 || n == 1) {
			return n;
		} else {
			int a = 0, b = 1, c = -1;
			for (int i = 2; i <= n; i++) { // gkq:从2开始计算
				c = a + b;
				a = b;
				b = c;
			}
			return c;
		}
	}
}
